/*
 * ChatPanel.java
 *
 * Created on 26.11.2007, 18:20:46
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.gui.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.text.StyleContext;

import cz.possoft.client.gui.panel.chatpanel.components.ChatTextPane;
import cz.possoft.client.gui.panel.chatpanel.components.SmajlPanel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;
import layout.TableLayout;

/**
 *
 * @author mara
 *
 * spodni panel kde spolu hraci chatuji
 */
public class ChatPanel extends JPanel {

    private static ChatTextPane chatTextPane;
    private JTextField chatTextField;
    private static JButton sendButton;

    double[][] size = {{20, 490, 20, 100, 20}, {0, 120, 30, 20, 10}};

    public ChatPanel() {
        initComponents();
    }

    public void initComponents() {
        setLayout(new TableLayout(size));
        chatTextPane = new ChatTextPane(new StyleContext());
        add(chatTextPane.getJsp(), "1,1,3,1");
        chatTextField = new JTextField();
        chatTextField.addKeyListener(new EnterKeyListener());
        chatTextPane.setEditable(false);
        add(chatTextField, "1,3");
        sendButton = new JButton(PropertiesManager.getInstance().getMessageForKey(Text.SEND));
        sendButton.addActionListener(new SendButtonListener());
        sendButton.setEnabled(false);
        add(sendButton, "3,3");
        add(new SmajlPanel(chatTextField), "1,2");
    }

    public static JButton getSendButton() {
        return sendButton;
    }

    public static ChatTextPane getChatTextPane() {
        return chatTextPane;
    }

    private class SendButtonListener implements ActionListener {

        // stisk tlactika 'odeslat'
        public void actionPerformed(ActionEvent ae) {
            NastaveniShowPanel.getConnectThread().sendToServer("SEND_MESSAGE: " + chatTextField.getText());
            chatTextField.setText("");
        }
    }

    private class EnterKeyListener extends KeyAdapter {

        
        public void keyPressed(KeyEvent ke) {
            if ((ke.getKeyCode() == KeyEvent.VK_ENTER) && (getSendButton().isEnabled())) {                
                getSendButton().getActionListeners()[0].actionPerformed(new ActionEvent(getSendButton(), ActionEvent.ACTION_PERFORMED, null));
            }
        }
    }
}
